clear
est clear

********************************************************************************
* sales

u if inrange(year, 2010, 2021) using "rawdata/0621_tobacco_R01_abbrev_append_parent", clear

recode month (1/3=1) (4/6=2) (7/9=3) (10/12=4), g(quarter)

g fips = fips_state_code
replace fips = 1731 if fips_state_code == 17 & fips_county_code == 31
replace fips = 1799 if fips_state_code == 17 & fips_county_code != 31
replace fips = 2431 if fips_state_code == 24 & fips_county_code == 31
replace fips = 2499 if fips_state_code == 24 & fips_county_code != 31

* drop observations with missing fips (612)
drop if fips == .

collapse 	(sum) 	anti_parent_sales_gum anti_parent_sales_lozenge anti_parent_sales_patch anti_parent_sales_total ENDS_parent_sales cig_parent_sales ///
					anti_parent_sales_gum_pl anti_parent_sales_lozenge_pl anti_parent_sales_patch_pl anti_parent_sales_total_pl anti_parent_sales_total_mg ///
			(mean) 	ENDS_parent_price cig_parent_price anti_parent_price_total anti_parent_price_gum anti_parent_price_lozenge anti_parent_price_patch fips ///
			, by(store_code_uc year quarter) fast

* rename the sales variables
loc vars1 "anti_parent_sales_gum anti_parent_sales_lozenge anti_parent_sales_patch anti_parent_sales_total ENDS_parent_sales cig_parent_sales ENDS_parent_price cig_parent_price anti_parent_sales_gum_pl anti_parent_sales_lozenge_pl anti_parent_sales_patch_pl anti_parent_sales_total_pl anti_parent_price_total anti_parent_price_gum anti_parent_price_lozenge anti_parent_price_patch anti_parent_sales_total_mg"
loc vars2 "nrt_gum nrt_loz nrt_patch nrt ends cigs ends_price cig_price nrt_gum_pl nrt_loz_pl nrt_patch_pl nrt_pl nrt_price nrt_gum_price nrt_loz_price nrt_patch_price nrt_mg_adj"
for zz in any `vars1' \ yy in any `vars2': ren zz yy

* brand-name only = both - private label
g nrt_gum_b = nrt_gum - nrt_gum_pl
g nrt_loz_b = nrt_loz - nrt_loz_pl
g nrt_patch_b = nrt_patch - nrt_patch_pl
g nrt_b = nrt - nrt_pl

*create the per capita measure of NRT selling stores;
g any_nrt = 0
replace any_nrt = 1 if nrt != 0

egen nrt_stores = total(any_nrt), by(fips year quarter)
drop any_nrt

tempfile sales
sa `sales'

********************************************************************************
* county core

u "rawdata/county_core", clear

*indoor cigarette bans: workplace, restaurant, bar
g cig_ban= cig_TreatEfW_cnty/3 + cig_TreatEfR_cnty/3 + cig_TreatEfB_cnty/3
g ecig_ban= ecig_TreatEfW_cnty/3 + ecig_TreatEfR_cnty/3 + ecig_TreatEfB_cnty/3

ren state fips

replace fips = 1731 if fips == 17 & county == 31
replace fips = 1799 if fips == 17 & county != 31
replace fips = 2431 if fips == 24 & county == 31
replace fips = 2499 if fips == 24 & county != 31

g pop_vary = demo_popadult_cnty + demo_popkid_cnty

keep cig_tax_cnty tob21_cnty econ_unemploy_cnty demo_popadult_cnty demo_popkid_cnty demo_povall_cnty demo_income_cnty cig_ban ecig_ban fips county year quarter demo_urban_cnty pop_vary ecig_taxdist_cnty ecig_taxlowdist_cnty ecig_min_age_cnty ecig_taxstand2cons_y20_cty ecig_taxstand2consop_y20_cty ecig_tax_adval_cty ecig_tax_excise_cty ecig_tax_sales_cty ecig_tax_cont_cty ecig_tax_twotier_cty covid_cases_cnty covid_deaths_jhu_cnty covid_sch50_cnty covid_sch25_cnty covid_sch75_cnty

order fips county year quarter pop_vary

recode demo_urban_cnty (1 2=1) (3/6=0) (.=.), g(urban)
recode demo_urban_cnty (1 2=0) (3/6=1) (.=.), g(rural)

loc vars1 "cig_tax_cnty ecig_taxstand2cons_y20_cty ecig_taxstand2consop_y20_cty tob21_cnty econ_unemploy_cnty demo_popadult_cnty demo_popkid_cnty demo_povall_cnty demo_income_cnty ecig_min_age_cnty covid_cases_cnty covid_deaths_jhu_cnty covid_sch50_cnty covid_sch25_cnty covid_sch75_cnty"
loc vars2 "cig_tax_county ecig_tax_county ecig_opentax_county tob21 uerate adults kids proverty income ecig_mlsa covid_case covid_death covid_sch50 covid_sch25 covid_sch75"
for zz in any `vars1' \ yy in any `vars2': ren zz yy

collapse (mean) cig_tax_county ecig_tax_county ecig_opentax_county tob21 uerate adults kids proverty income cig_ban ecig_ban rural urban ecig_taxdist_cnty ecig_taxlowdist_cnty ecig_mlsa ecig_tax_adval_cty ecig_tax_excise_cty ecig_tax_sales_cty ecig_tax_cont_cty ecig_tax_twotier_cty covid_sch50 covid_sch25 covid_sch75 (rawsum) pop_vary covid_case covid_death [aw=pop_vary], by(fips year quarter) fast

g ecig_tax_county_nom = ecig_tax_county

replace ecig_mlsa = 1 if ecig_mlsa == .


* create time variable
loc nums1 "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30	31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48"
loc nums2 "2010 2010 2010 2010 2011 2011 2011 2011 2012 2012 2012 2012 2013 2013 2013 2013 2014 2014 2014 2014 2015 2015 2015 2015 2016 2016 2016 2016 2017 2017 2017 2017 2018	2018 2018 2018 2019 2019 2019 2019 2020 2020 2020 2020 2021 2021 2021 2021"
loc nums3 "1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4"

g time = .
for zz in any `nums1' \ yy in any `nums2' \ xx in any `nums3': replace time = zz if year == yy & quarter == xx

g ecig_closedtax_county = ecig_tax_county - ecig_opentax_county

foreach type in "" open closed {
	* event study indicators
	so fips year quarter
	g eff_time`type' = time if ecig_`type'tax_county != 0 & ecig_`type'tax_county[_n-1] == 0 & fips == fips[_n-1]
	replace eff_time`type' = eff_time`type'[_n-1] if eff_time`type'[_n-1] != . & fips == fips[_n-1]
	gsort fips -time
	replace eff_time`type' = eff_time`type'[_n-1] if eff_time`type'[_n-1] != . & fips == fips[_n-1]
	so fips time

	* relative time
	g rel_time`type' = time - eff_time`type'

	* indicators -8 to +8
	recode rel_time`type' (-100/-8=1) (-7/100 .=0), g(time`type'_m8)
	recode rel_time`type' (-7=1) (-100/-8 -6/100 .=0), g(time`type'_m7)
	recode rel_time`type' (-6=1) (-100/-7 -5/100 .=0), g(time`type'_m6)
	recode rel_time`type' (-5=1) (-100/-6 -4/100 .=0), g(time`type'_m5)
	recode rel_time`type' (-4=1) (-100/-5 -3/100 .=0), g(time`type'_m4)
	recode rel_time`type' (-3=1) (-100/-4 -2/100 .=0), g(time`type'_m3)
	recode rel_time`type' (-2=1) (-100/-3 -1/100 .=0), g(time`type'_m2)
	recode rel_time`type' (-1=1) (-100/-2 0/100 .=0), g(time`type'_m1)
	recode rel_time`type' (0=1) (-100/-1 1/100 .=0), g(time`type'_p0)
	recode rel_time`type' (1=1) (-100/0 2/100 .=0), g(time`type'_p1)
	recode rel_time`type' (2=1) (-100/1 3/100 .=0), g(time`type'_p2)
	recode rel_time`type' (3=1) (-100/2 4/100 .=0), g(time`type'_p3)
	recode rel_time`type' (4=1) (-100/3 5/100 .=0), g(time`type'_p4)
	recode rel_time`type' (5=1) (-100/4 6/100 .=0), g(time`type'_p5)
	recode rel_time`type' (6=1) (-100/5 7/100 .=0), g(time`type'_p6)
	recode rel_time`type' (7=1) (-100/6 8/100 .=0), g(time`type'_p7)
	recode rel_time`type' (8/100=1) (-100/7 .=0), g(time`type'_p8)

	loc vars "time`type'_m8 time`type'_m7 time`type'_m6 time`type'_m5 time`type'_m4 time`type'_m3 time`type'_m2 time`type'_m1 time`type'_p0 time`type'_p1 time`type'_p2 time`type'_p3 time`type'_p4 time`type'_p5 time`type'_p6 time`type'_p7 time`type'_p8"
	loc labs ""-8" "-7" "-6" "-5" "-4" "-3" "-2" "-1" "0" "+1" "+2" "+3" "+4" "+5" "+6" "+7" "+8""
	for zz in any `vars' \ yy in any `labs': lab var zz "yy"

	egen max = max(rel_time`type'), by(fips)
	egen min = min(rel_time`type'), by(fips)

	g balance_down`type' = 0
	replace balance_down`type' = 1 if inrange(min, -44, -8)
	g balance_up`type' = 0
	replace balance_up`type' = 1 if inrange(max, 8, 45)

	drop min max
}
drop time

g state = "."
loc names "AK AL AR AZ CA CO CT DC DE FL GA HI IA ID IL IN KS KY LA MA MD ME MI MN MO MS MT NC ND NE NH NJ NM NV NY OH OK OR PA RI SC SD TN TX UT VA VT WA WI WV WY CookCo MontCo"
loc nums "2 1 5 4 6 8 9 11 10 12 13 15 19 16 1799 18 20 21 22 25 2499 23 26 27 29 28 30 37 38 31 33 34 35 32 36 39 40 41 42 44 45 46 47 48 49 51 50 53 55 54 56 1731 2431"
for zz in any `names' \ yy in any `nums': replace state = "zz" if fips == yy

* never treated group
gen never_treat = 0
loc never "AK AL AR AZ FL HI IA ID IN MI MO MS MT ND NE OK RI SC SD TN TX"
for zz in any `never': replace never_treat = 1 if state == "zz"

* cohorts 
loc cohort "3 22 23 24 25 27 29 30 33 36 39 40 41 42 43 45"
for zz in any `cohort': g cohortzz = 0

replace cohort3 = 1 if state == "MN"
replace cohort22 = 1 if state == "NC"
replace cohort23 = 1 if (state == "LA" | state == "MontCo")
replace cohort24 = 1 if state == "DC"
replace cohort25 = 1 if state == "CookCo"
replace cohort27 = 1 if (state == "WV" | state == "PA")
replace cohort29 = 1 if state == "KS"
replace cohort30 = 1 if state == "CA"
replace cohort33 = 1 if state == "DE"
replace cohort36 = 1 if state == "NJ"
replace cohort39 = 1 if (state == "IL" | state == "NM" | state == "VT")
replace cohort40 = 1 if (state == "CT" | state == "NY" | state == "OH" | state == "WA" | state == "WI")
replace cohort41 = 1 if (state == "NH" | state == "ME" | state == "NV")
replace cohort42 = 1 if state == "MA"
replace cohort43 = 1 if (state == "VA" | state == "WY" | state == "KY" | state == "UT")
replace cohort45 = 1 if (state == "CO" | state == "GA" | state == "MD" | state == "OR")

tempfile county
sa `county'

********************************************************************************
* state core

u "rawdata/state_core", clear

g demo_male_st = 1 - demo_female_st

* convert the indoor vaping and smoking bans to any FULL ban
loc vars1 "cig_workplace_restrict_st cig_restaurant_restrict_st cig_bar_restrict_st ecig_workplace_restrict_st ecig_restaurant_restrict_st ecig_bar_restrict_st"
loc vars2 "cig_work_ban cig_rest_ban cig_bar_ban ecig_work_ban ecig_rest_ban ecig_bar_ban"
for zz in any `vars1' \ yy in any `vars2': recode zz (2=1) (0 1=0), g(yy)

loc names ""Cigarette ban at worksites" "Cigarette ban at bars" "Cigarette ban at restaurants" "E-cigarette ban at worksites" "E-cigarette ban at bars" "E-cigarette ban at restaurants""
for zz in any `vars2' \ yy in any `names': lab var zz "yy"

recode ecig_lic_st (0=0) (1/4=1), g(ecig_lic)
recode tob_lic_st (0=0) (1/4=1), g(tob_lic)
drop ecig_lic_st tob_lic_st

ren alc_beer_tax_st beer_tax_st
ren alc_liquor_tax_st liq_tax_st
ren state fips
ren ACA medicaid_expansion
ren econ_cpi cpi
ren econ_unemploy_st unemployrate
ren ecig_tax_excise_st ecig_tax_excise
ren ecig_tax_percent_st e_cig_percent

* recode the Medicaid coverage of the services, coverage must be full (so yes)
loc mcaid_vars "mcaid_cig_gum_ffs mcaid_cig_gum_mc mcaid_cig_lozenge_ffs mcaid_cig_lozenge_mc mcaid_cig_patch_ffs mcaid_cig_patch_mc"
for zz in any `mcaid_vars': recode zz (1=1) (0 2 3/5=0), g(zz_t)

* cover product in FFS or MC Medicaid (some states do not have both programs)
egen mcaid_gum = rowmax(mcaid_cig_gum_ffs_t mcaid_cig_gum_mc_t)
lab var mcaid_gum "Medicaid covers NRT gum in FFS or MC"
egen mcaid_loz = rowmax(mcaid_cig_lozenge_ffs_t mcaid_cig_lozenge_mc_t)
lab var mcaid_loz "Medicaid covers NRT lozenge in FFR or MC"
egen mcaid_patch= rowmax(mcaid_cig_patch_ffs_t mcaid_cig_patch_mc_t)
lab var mcaid_patch "Medicaid covers NRT patch in FFS or MC"
drop *_t

* combine the temporary and permanment e-cigarette bans into one variable
egen ban_ecigs = rowmax(ecig_tempban_st ecig_permban_st)
lab var ban_ecigs "Temporary or permanent e-cigarettte ban"

* locate first cigarette tax increase in the panel, this is now the first increase that is 50 cents or more, not defined if there was no such hike
g cig_tax_st_nom = cig_tax_st
* create a time variable
loc nums1 "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48"
loc nums2 "2010 2010 2010 2010 2011 2011 2011 2011 2012 2012 2012 2012 2013 2013 2013 2013 2014 2014 2014 2014 2015 2015 2015 2015 2016 2016 2016 2016 2017 2017 2017 2017 2018	2018 2018 2018 2019 2019 2019 2019 2020 2020 2020 2020 2021 2021 2021 2021"
loc nums3 "1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4"

g time = .
for zz in any `nums1' \ yy in any `nums2' \ xx in any `nums3': replace time = zz if year == yy & quarter == xx

so fips year quarter
g dif_cig = cig_tax_st_nom - cig_tax_st_nom[_n-1] if fips == fips[_n-1]
g cig_tax_inc = 0
replace cig_tax_inc = 1 if dif_cig >= 0.50 & dif_cig != .

g test = time if cig_tax_inc == 1
egen eff_time_cig_tax = min(test), by(fips)
drop test cig_tax_inc
lab var eff_time_cig_tax "Time of first nominal state cigarette tax change of 50 cents or more (following JRU)"

* cigaratte tax increase relative time;
g rel_time_cig_tax = time-eff_time_cig_tax

recode rel_time_cig_tax (-100/-8=1) (-7/100 .=0), g(time_c_m8)
recode rel_time_cig_tax (-7=1) (-100/-8 -6/100 .=0), g(time_c_m7)
recode rel_time_cig_tax (-6=1) (-100/-7 -5/100 .=0), g(time_c_m6)
recode rel_time_cig_tax (-5=1) (-100/-6 -4/100 .=0), g(time_c_m5)
recode rel_time_cig_tax (-4=1) (-100/-5 -3/100 .=0), g(time_c_m4)
recode rel_time_cig_tax (-3=1) (-100/-4 -2/100 .=0), g(time_c_m3)
recode rel_time_cig_tax (-2=1) (-100/-3 -1/100 .=0), g(time_c_m2)
recode rel_time_cig_tax (-1=1) (-100/-2 0/100 .=0), g(time_c_m1)
recode rel_time_cig_tax (0=1) (-100/-1 1/100 .=0), g(time_c_p0)
recode rel_time_cig_tax (1=1) (-100/0 2/100 .=0), g(time_c_p1)
recode rel_time_cig_tax (2=1) (-100/1 3/100 .=0), g(time_c_p2)
recode rel_time_cig_tax (3=1) (-100/2 4/100 .=0), g(time_c_p3)
recode rel_time_cig_tax (4=1) (-100/3 5/100 .=0), g(time_c_p4)
recode rel_time_cig_tax (5=1) (-100/4 6/100 .=0), g(time_c_p5)
recode rel_time_cig_tax (6=1) (-100/5 7/100 .=0), g(time_c_p6)
recode rel_time_cig_tax (7=1) (-100/6 8/100 .=0), g(time_c_p7)
recode rel_time_cig_tax (8/100=1) (-100/7 .=0), g(time_c_p8)

loc vars "time_c_m8 time_c_m7 time_c_m6 time_c_m5 time_c_m4 time_c_m3 time_c_m2 time_c_m1 time_c_p0 time_c_p1 time_c_p2 time_c_p3 time_c_p4 time_c_p5 time_c_p6 time_c_p7 time_c_p8 "
loc labs ""-8" "-7" "-6" "-5" "-4" "-3" "-2" "-1" "0" "+1" "+2" "+3" "+4" "+5" "+6" "+7" "+8""
for zz in any `vars' \ yy in any `labs': lab var zz "yy"

egen max = max(rel_time_cig_tax), by(fips)
egen min = min(rel_time_cig_tax), by(fips)

g balance_down_cig = 0
replace balance_down_cig = 1 if inrange(min,-42,-8)
g balance_up_cig = 0
replace balance_up_cig = 1 if inrange(max,8,41)

drop min max
drop time

replace fips = 1799 if fips == 17
replace fips = 2499 if fips == 24

tempfile state
sa `state'

keep if fips == 1799 | fips == 2499
replace fips = 1731 if fips == 1799
replace fips = 2431 if fips == 2499

append using `state'
sa `state', replace

********************************************************************************
* merge

u `sales', clear

mer m:1 fips year quarter using `county', nogen keep(3)
mer m:1 fips year quarter using `state', nogen keep(3)

* create time variable
loc nums1 "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48"
loc nums2 "2010 2010 2010 2010 2011 2011 2011 2011 2012 2012 2012 2012 2013 2013 2013 2013 2014 2014 2014 2014 2015 2015 2015 2015 2016 2016 2016 2016 2017 2017 2017 2017 2018	2018 2018 2018 2019 2019 2019 2019 2020 2020 2020 2020 2021 2021 2021 2021"
loc nums3 "1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4"

g time = .
for zz in any `nums1' \ yy in any `nums2' \ xx in any `nums3': replace time = zz if year == yy & quarter == xx

* create the full cigarette tax, sum of federal, state, and local taxes
egen cig_tax = rowtotal(cig_tax_st cig_tax_fed cig_tax_county)
lab var cig_tax "Total cigarette tax (county, state, and federal)"

keep if inrange(year, 2010, 2021)

* convert financial variables from nominal to real terms
su cpi if year == 2020 & quarter == 4
g base = r(mean)
g cpi20 = cpi/base

su ecig_tax_county
loc nom_val "cig_tax_county ecig_tax_county cig_tax_st cig_tax_fed ecig_tax_excise tob_cigar_tax_st tob_littlecigar_tax_st beer_tax cig_tax income ends_price cig_price beer_tax_st liq_tax_st nrt_price nrt_loz_price nrt_patch_price"
for zz in any `nom_val': replace zz = zz/cpi20

su ecig_tax_county

* enactment date (for states) from CDC

***Updated 7/4/2022 by Catherine, need to double check***
g enact = 0
* California 10/31/2016
replace enact = 1 if state == "CA" & quarter == 4 & year == 2016
* Colorado 7/8/2020
replace enact = 1 if state == "CO" & quarter == 3 & year == 2020
* Connecticut 6/26/2019
replace enact = 1 if state == "CT" & quarter == 3 & year == 2019
* Delaware 7/3/2017
replace enact = 1 if state == "DE" & quarter == 3 & year == 2017
* District of Columbia 8/11/2015
replace enact = 1 if state == "DC" & quarter == 3 & year == 2015
* Georgia 7/22/2020
replace enact = 1 if state == "GA" & quarter == 3 & year == 2020
* Illinois 6/28/2019
replace enact = 1 if state == "IL" & quarter == 3 & year == 2019
* Indiana 4/29/2021
replace enact = 1 if state == "IN" & quarter == 2 & year == 2021
* Kansas 6/16/2015
replace enact = 1 if state == "KS" & quarter == 3 & year == 2015
* Kentucky 4/13/2020
replace enact = 1 if state == "KY" & quarter == 2 & year == 2020
* Louisiana 6/19/2015
replace enact = 1 if state == "LA" & quarter == 3 & year == 2015
* Maine 7/2/2019
replace enact = 1 if state == "ME" & quarter == 3 & year == 2019
* Maryland 2/12/2021
replace enact = 1 if state == "MD" & quarter == 1 & year == 2021
* Massachusetts 11/27/2019
replace enact = 1 if state == "MA" & quarter == 4 & year == 2019
* Minnesota 5/11/2010
replace enact = 1 if state == "MN" & quarter == 2 & year == 2010
* Nevada 6/12/2019
replace enact = 1 if state == "NV" & quarter == 2 & year == 2019
* New Hampshire 9/26/2019
replace enact = 1 if state == "NH" & quarter == 3 & year == 2019
* New Jersey 7/1/2018
replace enact = 1 if state == "NJ" & quarter == 3 & year == 2018
* New Mexico 4/4/2019
replace enact = 1 if state == "NM" & quarter == 2 & year == 2019
* New York 4/12/2019
replace enact = 1 if state == "NY" & quarter == 2 & year == 2019
* North Carolina 5/29/2014
replace enact = 1 if state == "NC" & quarter == 2 & year == 2014
* Ohio 7/18/2019
replace enact = 1 if state ==" OH" & quarter == 3 & year == 2019
* Oregon 11/2/2020
replace enact = 1 if state == "OR" & quarter == 4 & year == 2020
* Pennsylvania 7/13/2016
replace enact = 1 if state == "PA" & quarter == 3 & year == 2016
* Utah 3/30/2020
replace enact = 1 if state == "UT" & quarter == 2 & year == 2020
* Vermont 5/23/2019
replace enact = 1 if state == "VT" & quarter == 2 & year == 2019
* Virginia 5/21/2020
replace enact = 1 if state == "VA" & quarter == 2 & year == 2020
* Washington 5/21/2019
replace enact = 1 if state == "WA" & quarter == 2 & year == 2019
* West Virginia 6/13/2016
replace enact = 1 if state == "WV" & quarter == 2 & year == 2016
* Wisconsin 7/3/2019
replace enact = 1 if state == "WI" & quarter == 3 & year == 2019
* Wyoming 3/10/2020
replace enact = 1 if state == "WY" & quarter == 1 & year == 2020

* counties (from Mike)
* Cook Co IL 11/18/2015
replace enact = 1 if state == "CookCo" & quarter == 4 & year == 2015
* Mont Co MD 4/14/2015
replace enact = 1 if state == "MontCo" & quarter == 2 & year == 2015

so fips year quarter
replace enact = 1 if enact[_n-1] == 1 & fips == fips[_n-1] & ecig_tax_county == 0

* set ends to missing before 2011, the first year we can track them
replace ends = . if year < 2011

* interact tax with distance
g ecig_taxlowdist_cnty_tax = ecig_taxlowdist_cnty*ecig_tax_county
g ecig_taxdist_cnty_tax = ecig_taxdist_cnty*ecig_tax_county

* division-by-period FE
g div = 0
* division 1, Division 1: New England (Connecticut, Maine, Massachusetts, New Hampshire, Rhode Island, and Vermont)
replace div = 1 if inlist(state, "CT", "ME", "MA", "NH", "RI", "VT")
* division 2: Mid-Atlantic (New Jersey, New York, and Pennsylvania)
replace div = 2 if inlist(state, "NJ", "NY", "PA")
* division 3: East North Central (Illinois, Cook County, Indiana, Michigan, Ohio, and Wisconsin)
replace div = 3 if inlist(state, "IL", "CookCo", "IN", "MI", "OH", "WI")
* division 4: West North Central (Iowa, Kansas, Minnesota, Missouri, Nebraska, North Dakota, and South Dakota)
replace div = 4 if inlist(state, "IA", "KS", "MN", "MO", "NE", "ND", "SD")
* division 5: South Atlantic (Delaware, Florida, Georgia, Maryland, Montgomery County, North Carolina, South Carolina, Virginia, District of Columbia, and West Virginia)
replace div = 5 if inlist(state, "DE", "FL", "GA", "MD", "MontCo", "NC", "SC", "VA", "DC") | inlist(state, "WV")
* division 6: East South Central (Alabama, Kentucky, Mississippi, and Tennessee)
replace div = 6 if inlist(state, "AL", "KY", "MS", "TN")
* division 7: West South Central (Arkansas, Louisiana, Oklahoma, and Texas)
replace div = 7 if inlist(state, "AR", "LA", "OK", "TX")
* division 8: Mountain (Arizona, Colorado, Idaho, Montana, Nevada, New Mexico, Utah, and Wyoming)
replace div = 8 if inlist(state, "AZ", "CO", "ID", "MT", "NV", "NM", "UT", "WY")
* division 9: Pacific (Alaska, California, Hawaii, Oregon, and Washington)
replace div = 9 if inlist(state, "AK", "CA", "HI", "OR", "WA")

egen div_time = group(div time)

egen any_mcaid_cov = rowmax(mcaid_gum mcaid_loz mcaid_patch)

* number of periods stores are oberved
g n = 1
egen periods = total(n), by(store_code_uc)
lab var periods "Number of periods a store is observed in the data"

* any NRT sales
g any_nrt = 0
replace any_nrt = 1 if nrt != 0
lab var any_nrt "Any NRT sales at store"

* any ENDS sales
g any_ends = 0
replace any_ends = 1 if ends != 0
lab var any_ends "Any ENDS sales at store"

* periods of NRT sales at stores
egen periods_nrt = total(any_nrt), by(store_code_uc)
lab var periods_nrt "Number of periods a stores has positive NRT sales"

* periods of NRT sales at stores
egen periods_ends = total(any_ends), by(store_code_uc)
lab var periods_ends "Number of periods a stores has positive ENDS sales"

* store-level sales in Q1 and Q2 in 2010, period before there are any e-cigarette taxes in the U.S.
gen sales_2010q1 = nrt if year == 2010 & quarter == 1
gen sales_2010q2 = nrt if year == 2010 & quarter == 2
so store_code_uc year quarter
replace sales_2010q1 = sales_2010q1[_n-1] if sales_2010q1 == . & store_code_uc == store_code_uc[_n-1]

replace sales_2010q2 = sales_2010q2[_n-1] if sales_2010q2 == . & store_code_uc == store_code_uc[_n-1]
gsort store_code_uc -year -quarter
replace sales_2010q2 = sales_2010q2[_n-1] if sales_2010q2 == . & store_code_uc == store_code_uc[_n-1]
so store_code_uc year quarter

g sales_2010 = (sales_2010q1 + sales_2010q2)/2
lab var sales_2010 "Store-level sales in Q1 and Q2 2010"

* population in 2010
gen temp = pop_vary if year == 2010 & quarter == 1
bysort fips: egen pop_2010q1 = max(temp)
drop temp

lab var pop_2010q1 "Jurisdiction-level population in 2010 Q1"

lab var ecig_tax_county "E-cigarette tax"

*stores selling NRTs per capita, using full sample of strores in NRSD
g nrt_stores_pc = nrt_stores/pop_vary

replace nrt_b = . if year == 2021
replace nrt_pl = . if year == 2021

sa "workingdata/nrt_tax_clean_stores", replace

import excel using "rawdata\COVID-19_US_state_policy_database.xlsx", clear sheet(Closures & Reopening) firstrow
drop if _n > 51

keep StateFIPSCode Closedothernonessentialbusin Reopenedothernonessentialret 
rename State fips
gen yclose = year(Closed)
gen qclose = quarter(Closed)
gen yqclose = yq(yclose, qclose)
format yqclose %tq

gen yopen = year(Reopen)
gen qopen = quarter(Reopen)
gen yqopen = yq(yopen, qopen)
format yqopen %tq

keep fips yqclose yqopen

expand 2 if inlist(fips, 17, 24)
bysort fips: gen count = _n
replace fips = 1731 if fips == 17 & count == 1
replace fips = 1799 if fips == 17 & count == 2
replace fips = 2431 if fips == 24 & count == 1
replace fips = 2499 if fips == 24 & count == 2
drop count

tempfile covidclosure
save `covidclosure', replace

u "workingdata/nrt_tax_clean_stores", clear
merge m:1 fips using `covidclosure', nogen
gen yqtime = yq(year, quarter)
gen covid_closure = inrange(yqtime, yqclose, yqopen)

sa "workingdata/nrt_tax_clean_stores", replace
